<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/18
 * Time: 14:41
 */
namespace app\api\controller;
use think\Db;
class Timing extends Base {
    //未回复超过24小时自动关闭
    public function index(){
        set_time_limit(0);
        //定时器1：解答超过1小时未提交则清除状态；定时器3：未答题的一小时后关闭
        $time = time();
        $one_hour_ago = date("Y-m-d H:i:s",($time - 3600));
        $where['course_status'] = 1;
        $where['course_time'] = ['<=',$one_hour_ago];
        $res = Db::table('up_course')->where($where)->select();
        if($res){
            foreach ($res as $key=>$vo){
                $dataTime = ($time-strtotime($vo['course_time']))/3600;
                if($vo['course_click_uid'] == 0){ //如果是没人答题超过24小时，改成关闭
                    if($dataTime>24){
                        Db::startTrans();
                        try{
                            Db::table('up_course')->where('course_id',$vo['course_id'])->setField('course_status',3);
                            //返还学生钱
                            Db::table('up_u_user')->where('user_id',$vo['course_user_id'])->setInc('user_balance',$vo['course_gold']);
                            //学生入order增并删除记录
                            allInOrder(2,$vo['course_gold'],$vo['course_user_id'],1);
                            Db::table('up_purchase')->where('purchase_type',4)->where('f_id',$vo['course_id'])->delete();
                            Db::commit();
                        }catch (\Exception $e) {
                            Db::rollback();
                        }
                    }
                }else{  //如果是有人答题超过1小时未提交，恢复原始状态
                    $clickTime = ($time-strtotime($vo['course_click_time']))/3600;
                    if($clickTime > 1){
                        Db::table('up_course')->where('course_id',$vo['course_id'])->setField('course_click_uid',0);
                    }
                }
            }
        }
        //定时器2：答完题一小时到账户
        $where2['course_status'] = 2;
        $where2['course_money'] = 1;
        $where2['course_answer_time'] = ['<=',$one_hour_ago];
        $res = Db::table('up_course')->alias('a')
            ->join('up_course_reply r','a.course_id = r.reply_course_id','left')
            ->where($where2)
            ->field('course_id,reply_user_id,course_gold,course_type')
            ->select();
        if($res){
            Db::startTrans();
            try{
                foreach ($res as $key=>$vo){
                    Db::table('up_course')->where('course_id',$vo['course_id'])->setField('course_money',2);
                    Db::table('up_u_user')->where('user_id',$vo['reply_user_id'])->setInc('user_balance',$vo['course_gold']);
                    //更新下公开求助的答题老师的进账记录
                    if($vo['course_type'] == 2){
                        Db::table('up_purchase')
                            ->where('purchase_type',4)
                            ->where('f_id',$vo['course_id'])
                            ->setField('teacher_id',$vo['reply_user_id']);
                    }
                    //
                    //老师入order
                    allInOrder(2,$vo['course_gold'],$vo['reply_user_id'],1);
                }
                Db::commit();
            }catch (\Exception $e) {
                Db::rollback();
            }
        }
//        一对一-----------------------------
        //超过1分钟没有接受的，关闭并返还钱
        $one_minute_ago = date("Y-m-d H:i:s",($time - 60));
        $pays = Db::table('up_one_to_one')
            ->where('one_status',1)
            ->where('one_time','<=',$one_minute_ago)
            ->field('one_id,one_student_id,one_teacher_id')
            ->select();
        if($pays){
            foreach ($pays as $k=>$vo){
                Db::startTrans();
                try{
                    Db::table('up_one_to_one')->where('one_id',$vo['one_id'])->setField('one_status',5);
                    //返还学生钱
                    $b_money = Db::table('up_u_user')->where('user_id',$vo['one_teacher_id'])->value('user_toll');
                    Db::table('up_u_user')->where('user_id',$vo['one_student_id'])->setInc('user_balance',$b_money);
                    //学生入order增并删除记录
                    allInOrder(3,$b_money,$vo['one_student_id'],1);
                    Db::table('up_purchase')->where('purchase_type',3)->where('f_id',$vo['one_id'])->delete();
                    Db::commit();
                }catch (\Exception $e) {
                    Db::rollback();
                }
            }
        }
        //支付完超过1小时没有开始的，关闭并返还钱
        $outs = Db::table('up_one_to_one')
            ->where('one_status',2)
            ->where('one_agree_pay_time','<=',$one_hour_ago)
            ->field('one_id,one_student_id,one_teacher_id')
            ->select();
        if($outs){
            foreach ($outs as $k=>$vo){
                Db::startTrans();
                try{
                    Db::table('up_one_to_one')->where('one_id',$vo['one_id'])->setField('one_status',5);
                    //返还学生钱
                    $b_money = Db::table('up_u_user')->where('user_id',$vo['one_teacher_id'])->value('user_toll');
                    Db::table('up_u_user')->where('user_id',$vo['one_student_id'])->setInc('user_balance',$b_money);
                    //学生入order增并删除记录
                    allInOrder(3,$b_money,$vo['one_student_id'],1);
                    Db::table('up_purchase')->where('purchase_type',3)->where('f_id',$vo['one_id'])->delete();
                    Db::commit();
                }catch (\Exception $e) {
                    Db::rollback();
                }
            }
        }
        //结束超过24小时把钱打给老师
        $hour_ag24 = date("Y-m-d H:i:s",($time - 3600*24));
        $teas = Db::table('up_one_to_one')
            ->where('one_status',4)
            ->where('one_end_status',1)
            ->where('one_end_time','<=',$hour_ag24)
            ->field('one_id,one_student_id,one_teacher_id')
            ->select();
        if($teas){
            foreach ($teas as $k=>$vo){
                Db::startTrans();
                try{
                    Db::table('up_one_to_one')->where('one_id',$vo['one_id'])->setField('one_end_status',2);
                    //给老师钱
                    $b_money = Db::table('up_u_user')->where('user_id',$vo['one_teacher_id'])->value('user_toll');
                    Db::table('up_u_user')->where('user_id',$vo['one_teacher_id'])->setInc('user_balance',$b_money);
                    //老师入order增
                    allInOrder(3,$b_money,$vo['one_teacher_id'],1);
                    Db::commit();
                }catch (\Exception $e) {
                    Db::rollback();
                }
            }
        }
    }
    public function dowload_stu() {//echo 1;die;
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
           // echo "<script> location.href='http://openbox.mobilem.360.cn/index/d/sid/4159000'; </script>";
            echo '学生端 ios 下载地址';
        }else{
           echo "<script> location.href='http://openbox.mobilem.360.cn/index/d/sid/4159000'; </script>";
        }

    }
    public function dowload_tea() {//echo 1;die;
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
           echo "<script> location.href='https://itunes.apple.com/cn/app/id1481737971'; </script>";
        }else{
           echo "<script> location.href='http://openbox.mobilem.360.cn/index/d/sid/4158999'; </script>";
        }

    }
}